  

    var mask;
    var share_mask;

    var detail = new Vue({
        el: '.body',
        data: {
            confirm_goods_is_show: false,
            goods_num: 1,
            is_collect: false,
            collect_icon: '../images/ptsp/icon_05.png',
            is_buy_car: false,
            buy_car_icon: '../images/ptsp/icon_07.png',
            to_say_height: '',
            to_info_height: '',
            share_box_is_show: false,
            wareInfoGroup: {},	// 商品信息
            commentGroup: {},	// 商品评论
            user: {},	// 用户
            collectStatus: false,	// 收藏状态
            currentSpecification: null,
            currentSpecificationIndex: -1,
            confirmType: 0,
            groupBuying: false,
            cartId: 0,
        },
        methods: {
            show_sel_box: function ({type:type,groupBuying:groupBuying}) {
                this.confirmType = type;
                this.groupBuying = groupBuying;
                if(this.confirmType == 0 && this.is_buy_car){
                    this.deleteCartById();
                    return false; 
                }
                this.confirm_goods_is_show = true;
                mask.show();
            },
            close_sel_box: function () {
                mask.close();
                this.confirm_goods_is_show = false;
            }
            , 
            cut_num: function () {
                if (this.goods_num >= 2) {
                    this.goods_num--;
                }
            },
            add_num: function () {
                if (this.wareInfoGroup.stock > this.goods_num) {
                    this.goods_num++;
                }
            }
            ,
            call_kefu: function () {
                mui.toast("联系客服")
            },
            collect: function () {
                let _this = this;
                let goodsid = this.wareInfoGroup.goodsid;
                let uid = this.user.uid;
                //  status(0 => 未收藏 1 => 收藏) type (0 商城)
                if (!_this.collectStatus) {
                    $.get(url + 'api/collect/addcollect', {
                        goodsid: goodsid,
                        uid: uid,
                        status: 0,
                        type: 0
                    }, function (response) {
                        let responseData = JSON.parse(response);
                        if (responseData.code == '1') {
                            let msg = responseData.msg;
                            mui.toast(msg)
                            _this.collectStatus = true;
                            _this.collect_icon = '../images/ptsp/icon2_05.png';
                        }
                    });
                } else {
                    $.get(url + 'api/collect/addcollect', {
                        goodsid: goodsid,
                        uid: uid,
                        status: 1,
                        type: 0
                    }, function (response) {
                        let responseData = JSON.parse(response);
                        if (responseData.code == '0') {
                            let msg = responseData.msg;
                            mui.toast(msg)
                            _this.collectStatus = false;
                            _this.collect_icon = '../images/ptsp/icon_05.png';
                        }
                    });
                }
            },
            specificationActive: function (index) { // 选择当前规格
                this.currentSpecification = this.wareInfoGroup.specification[index];
                this.currentSpecificationIndex = index;
            },
            to_say: function (e) {
            	console.log('to_say')
                var that = this;
                document.querySelector(".ptsp_active").className = "";
                e.target.className = 'ptsp_active'
                mui('.mui-scroll-wrapper').scroll().scrollTo(0, -that.to_say_height, 500);
            },
            to_detail: function (e) {
                document.querySelector(".ptsp_active").className = "";
				console.log('to_detail')
                e.target.className = 'ptsp_active'

                var that = this;
                // var pingjia_height = document.getElementById("pingjia").clientHeight;
                // var height = pingjia_height + 579;
                mui('.mui-scroll-wrapper').scroll().scrollTo(0, -that.to_info_height, 500);
            },
            to_goods: function (e) {
            	
            	console.log('to_goods')
                document.querySelector(".ptsp_active").className = "";

                e.target.className = 'ptsp_active'

                mui('.mui-scroll-wrapper').scroll().scrollTo(0, 0, 500);
            },
            select_color: function (e) {
                document.querySelector(".select_color .active").className = "item";
                e.target.className = 'item active';
                mui.toast("已选择:" + e.target.innerHTML)
            },
            deleteCartById: function () {
                let _this = this;
                $.get(url + 'api/cart/delete', {cartid: _this.cartId}, function (response) {
                    let responseData = JSON.parse(response);
                    if (responseData.code == '1') {
                        _this.buy_car_icon = '../images/ptsp/icon_07.png';
                        _this.is_buy_car = false;
                        mui.toast('已从购物车删除')
                    }
                });
            },
            go_confirm_oreder: function () {
                let _this = this;
                let confirmType = this.confirmType;
                if(confirmType == 0){   // 添加购物车
                    if (!_this.is_buy_car) { // 添加至购物车
                        let currentSpecification = this.currentSpecification;
                        if(!currentSpecification){
                            mui.toast("请选择商品规格" );
                            return false;
                        }
                        let rule = currentSpecification.rule;
                        let goodsid = currentSpecification.goodsid;
                        let num= this.goods_num;
                        let uid= this.user.uid;

                        $.post(url + 'api/cart/cartadd', {goodsid: goodsid,num:num,rule:rule,uid:uid}, function (response) {
                            let responseData = JSON.parse(response);
                            if (responseData.code == '1') {
                                let cartId = responseData.data;
                                _this.cartId = cartId;
                                _this.buy_car_icon = '../images/ptsp/icon2_07.png';
                                mui.toast('已添加至购物车')
                                _this.is_buy_car = true;
                            }
                        });
                    }
                    this.close_sel_box();
                }else if(confirmType == 1){ // 直接下单
                    let currentSpecification = this.currentSpecification;
                    if(!currentSpecification){
                        mui.toast("请选择商品规格" );
                        return false;
                    }
                    let cartInfo = {
                        rule: currentSpecification.rule,
                        goodsid: currentSpecification.goodsid,
                        num:this.goods_num,
                        uid:this.user.uid,
                        groupBuying: this.groupBuying,
                        attrid: currentSpecification.attrid,
                        spell:1,
                        spell_id: 0,
                    }
                    host_config.localStorage.setItem(host_config.cartInfo,JSON.stringify(cartInfo));
                    mui.openWindow('confirm_order.html?confirmType=1');
                }
            },
            publishOrder: function ({id:id,attrid:attrid,num:num}) { // 拼团
                let cartInfo = {
                    rule: '',
                    goodsid: this.wareInfoGroup.goodsid,
                    num:num,
                    uid:this.user.uid,
                    groupBuying: true,
                    attrid: attrid,
                    spell:0,
                    spell_id: id,
                }
                host_config.localStorage.setItem(host_config.cartInfo,JSON.stringify(cartInfo));
                mui.openWindow('confirm_order.html?confirmType=1');
            },
            open_share: function () {
                this.share_box_is_show = true;
                share_mask.show();
            }
        },
        mounted: function () {
            mui.init({
                swipeBack: true //启用右滑关闭功能
            });
           
            mui.plusReady(function(){
                plus.nativeUI.showWaiting('加载中' );
            })
            let goodsId = util.GetQueryString('goodsid');
            let user = host_config.localStorage.user;
            if (user) {
                this.user = JSON.parse(user);
            }
          
            // 获取商品信息
            $.get(url + 'app/goods/detail', {goodsid: goodsId}, function (response) {
                let responseData = JSON.parse(response);
                if (responseData.code == '1') {
                    let wareItem = responseData.data;
                    console.log(responseData)
                    detail.wareInfoGroup = wareItem;
                    detail.wareInfoGroup.firstPic = wareItem.pics[0];
                    detail.wareInfoGroup.lastPic = wareItem.pics[wareItem.pics.length - 1];
                }
                 
                setTimeout(function(){
                	height_init()//初始化顶部切换滚动距离
                    init_banner_num();//初始化轮播数字
                },500)
                if(window.plus){
                    plus.nativeUI.closeWaiting();
                }
            });

            // 获取评论
            $.get(url + 'api/comment/commentLists', {goodsid: goodsId}, function (response) {
                let responseData = JSON.parse(response);
                if (responseData.code == '1') {
                    let data = responseData.data;
                     
                    for (dataItem in data) {
                        let localeDate = new Date(data[dataItem].createtime).toLocaleDateString();
                        data[dataItem].createtime = localeDate.replace('//', '-')
                    }
                    detail.commentGroup = data;
                }
            });

            let _this = this;
            let uid = this.user.uid;
            // 获取默认收藏状态
            $.get(url + 'api/collect/iscollect', {
                goodsid: goodsId,
                uid: uid,
            }, function (response) {
                let responseData = JSON.parse(response);
                
                if (responseData.code == '1') {
                    _this.collectStatus = true;
                    _this.collect_icon = '../images/ptsp/icon2_05.png';
                } else {
                    _this.collectStatus = false;
                    _this.collect_icon = '../images/ptsp/icon_05.png';
                }
            });


            var that = this
            // 初始化蒙版遮罩
            mask = mui.createMask(function () {
                that.confirm_goods_is_show = false;
            });
            share_mask = mui.createMask(function () {
                that.share_box_is_show = false;
            });

            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
		  	slider_init()
            
            
            
         

           
        },
    })

	function height_init(){
		var that = detail
		//初始化 滑动高度
		   var banner_height = document.querySelector("#shangpin").clientHeight;
            var goods_info_height = document.querySelector("#goods_info").clientHeight;
            
            if(detail.wareInfoGroup.spell == 1){
            	var ptsp_users_height = document.querySelector(".ptsp_users").clientHeight;
            	console.log('ptsp_users_height->'+ptsp_users_height)
            }
            
            var hr_20_height = document.querySelector(".hr_20").clientHeight;
            var say_height = document.querySelector("#pingjia").clientHeight;
			console.log('banner_height->'+banner_height)
			console.log('goods_info_height->'+goods_info_height)
			
			console.log('hr_20_height->'+hr_20_height)
			console.log('say_height->'+say_height)
			if(detail.wareInfoGroup.spell == 1){
            	var ptsp_users_height = document.querySelector(".ptsp_users").clientHeight;
            	console.log('ptsp_users_height->'+ptsp_users_height)
            	that.to_say_height = banner_height + goods_info_height + hr_20_height + ptsp_users_height; 
            }else{
            	that.to_say_height = banner_height + goods_info_height + hr_20_height; 
            	
            }
            
            that.to_info_height = that.to_say_height + say_height + hr_20_height;
	}
    function slider_init() {
        var slider = mui("#slider");
        slider.slider({
            interval: 50000
        });
    }

    function init_banner_num() {
        

            var count = document.querySelectorAll("#slider .mui-slider-item").length;
            console.log(count);
            document.querySelector(".ptsp_pic_i").innerHTML = '1';
            console.log(111,detail.wareInfoGroup.pics.length)
            document.querySelector(".ptsp_pic_n").innerHTML =  detail.wareInfoGroup.pics.length
      
        document.querySelector('.mui-slider').addEventListener('slide', function (event) {

            document.querySelector(".ptsp_pic_i").innerHTML = event.detail.slideNumber + 1;

        });
    }

